﻿/* ----------------------------------------------------------------- */
/*                    Get project list by user                       */
/* ----------------------------------------------------------------- */
function GetProjectListByUserId(userId) {
    $.ajax({
        url: "/EnTimeLog/GetProjectPartialByUser",
        type: 'POST',
        data: { userId: userId },
        success: function (response) {
            $("#ajaxDropDownListProject").replaceWith(response);
            ClearGridData();
        },
        error: function (xhr) {
            alert("Error!");
        }
    });
}

/* ----------------------------------------------------------------- */
/*                    Get project data by user                       */
/* ----------------------------------------------------------------- */
function GetProjectData() {
    if ($("select#ddlProject").val() != "-1" && $("select#ddlProject").val() != "" && $("select#ddlProject").val() != null) {

        // Get project referenced value
        var pr_ref = $("select#ddlProject").val();

        // Ajax function
        $.ajax({
            url: "/ManageTask/GetTask",
            type: 'POST',
            data: {
                projectId: pr_ref
            },
            success: function (response) {
                $("#ajaxGridTask").replaceWith(response)
            },
            error: function (xhr) {
                alert("Error!");
            }
        });

    }
    else {
        // Clear all grid's data
        ClearGridData();
    }
}

/* ----------------------------------------------------------------- */
/*                    Clear all grid                                 */
/* ----------------------------------------------------------------- */
/*  Clear all grid data    */
function ClearGridData() {
    $("#ajaxGridCategory").replaceWith("<div id='ajaxGridCategory'></div>");
    $("#ajaxGridTask").replaceWith("<div id='ajaxGridTask'></div>");
    $("#ajaxListOtherEngineers").replaceWith("<div id='ajaxListOtherEngineers'></div>");
}

/* ----------------------------------------------------------------- */
/*                    Hide edit panel                                */
/* ----------------------------------------------------------------- */
/*  Clear all grid data    */
function HideEditPanel() {
    $("#editTaskDiv").replaceWith("<div id='editTaskDiv'></div>");
}

/* ----------------------------------------------------------------- */
/*                    Open loading dialog                            */
/* ----------------------------------------------------------------- */
function GetLoadingDialog() {
    var dialog = $('#LoadingDialog');
    dialog.html('<div><img src="../../Images/ajax-loader.gif" /></div>');
    dialog.dialog({
        closeOnEscape: false,
        dialogClass: "no-title",
        bgiframe: true,
        resizable: false,
        modal: true,
        width: 250,
        height: 50
    });

}

/* ----------------------------------------------------------------- */
/*                    Close loading dialog                           */
/* ----------------------------------------------------------------- */
function CloseLoadingDialog() {
    var dialog = $('#LoadingDialog');
    dialog.dialog("close");
}

/* ----------------------------------------------------------------- */
/*                    Add new task                                   */
/* ----------------------------------------------------------------- */
function AddNewProjectTask( _taskName, _projectId, _userId, _startDate, _endDate) {
    // Ajax function
    GetLoadingDialog();
    $.ajax({
        url: "/ManageTask/AddNewProjectTask",
        type: 'POST',
        data: {
            taskName: _taskName,
            projectId: _projectId,
            listUserId: _userId,
            startDate: _startDate,
            endDate: _endDate
        },
        success: function (response) {
            alert('Successfully added new task');
            GetProjectData();
            CloseLoadingDialog();

        },
        error: function (xhr) {
            CloseLoadingDialog();
            alert("Error!");

        }
    });
}

/* ----------------------------------------------------------------- */
/*                    Set project lead engineer                      */
/* ----------------------------------------------------------------- */
function SetLeadEngineer(_projectId, _userId, _projectName, _userName) {
    // Ajax function
    GetLoadingDialog();

    $.ajax({
        url: "/ManageTask/SetProjectLeadEngineer",
        type: 'POST',
        data: {
            projectId: _projectId,
            userId: _userId
        },
        success: function (response) {
            alert('Successfully set lead engineer !');
            CloseLoadingDialog();

        },
        error: function (xhr) {
            CloseLoadingDialog();
            alert("Set lead engineer failed!");

        }
    });
}

/* ----------------------------------------------------------------- */
/*                    Load task to edit                              */
/* ----------------------------------------------------------------- */
function LoadTaskToEdit(_taskId) {
    // Ajax function
    GetLoadingDialog();

    $.ajax({
        url: "/ManageTask/LoadTaskToEdit",
        type: 'POST',
        data: {
            taskId: _taskId
        },
        success: function (response) {
            $("#editTaskDiv").replaceWith(response);
            CloseLoadingDialog();

        },
        error: function (xhr) {
            CloseLoadingDialog();
            alert("Error in loading task!");
        }
    });
}

/* ----------------------------------------------------------------- */
/*                    Edit a task                                    */
/* ----------------------------------------------------------------- */
function EditProjectTask(_taskId, _taskName, _projectId, _userId, _startDate, _endDate) {
    // Ajax function
    GetLoadingDialog();
    $.ajax({
        url: "/ManageTask/EditProjectTask",
        type: 'POST',
        data: {
            taskId: _taskId,
            taskName: _taskName,
            projectId: _projectId,
            listUserId: _userId,
            startDate: _startDate,
            endDate: _endDate
        },
        success: function (response) {
            alert('Successfully edited a task');
            GetProjectData();
            CloseLoadingDialog();

        },
        error: function (xhr) {
            CloseLoadingDialog();
            alert("Error in editing task!");

        }
    });
}

/* ----------------------------------------------------------------- */
/*                    Delete a task                                    */
/* ----------------------------------------------------------------- */
function DeleteProjectTask(_taskId) {
    // Ajax function
    GetLoadingDialog();
    $.ajax({
        url: "/ManageTask/DeleteProjectTask",
        type: 'POST',
        data: {
            taskId: _taskId
        },
        success: function (response) {
            alert('Successfully deleted a task');
            GetProjectData();
            CloseLoadingDialog();
            HideEditPanel();

        },
        error: function (xhr) {
            CloseLoadingDialog();
            alert("Error in deleting task!");
        }
    });
}